<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ include file="/pages/inc/taglibs.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title><fmt:message key="sonarservices.list.title"/></title>
	<%@ include file="/pages/inc/header.jsp" %>
	<style>
	#ptstool{color:blue}
	</style>
</head>

<body>
<%@ include file="/pages/inc/common.jsp" %>
<%@ include file="/pages/inc/menu.jsp" %>

<div class="pts-main">
	<%@ include file="/pages/inc/nav_pm.jsp" %>
	<input type="hidden"  id="projectId" value='<c:out value="${projectId}"/>'/>
	<div class="pts-page-title"><span>已开通工具服务</span></div>
	<div class="container-fluid">
		<div class="barTitle_opened"></div>
	</div>
	<div class="pts-page-title"><span>未上线工具服务</span></div>
	<div class="container-fluid">
		<div class="barTitle_Notopened"></div>
	</div>
	<div class="pts-page-title"><span>工具服务使用记录</span></div>
	<div class="container-fluid">
		<!-- 
		<form id="sonarservicesForm"  class="bizform">
			<div class="row"> 
				<div class="querycell">
					<button id="query_button" class="button" type="button" onclick="query();"><fmt:message key='button.search'/></button>
				</div>
			</div>
		</form>
		 -->
		<div class="ui-table ui-margin">
			<div class="nav">
				<!-- <button id="addBtn" class="button" type="button" onclick="add();"><fmt:message key='button.add'/></button>
				<button id="editBtn" class="button" type="button" onclick="edit();"><fmt:message key='button.edit'/></button> -->
			   <button id="viewBtn" class="button" type="button" onclick="view();"><fmt:message key='button.view'/></button>
			   <button id="deleteBtn" class="button" type="button" onclick="batchDelete();"><fmt:message key='button.delete'/></button>
			</div>
			<table id="sonarservicesTable"></table>
			<div id="sonarservicesPager"></div>
		</div>
	</div>
</div>

<!-- 引入尾文件-->
<%@ include file="/pages/inc/footer.jsp" %>
<script type="text/javascript">
	var sonarservicesGrid;
	window.onload = function showToolInfo(){
		var openhtml = "";
		var notopenhtml = "";
		$.ajax({
			url: contextPath+"/sonarservices/list",
			type:"POST",
			cache:false,
			async:false,
			success: function(data, textStatus, jqXHR){
				for(var i=0;i<data.length;i++){
					if(data[i].status == "1"){//上线
						openhtml +='<div serviceId="'+data[i].id+'" class="pts-prjlist-items" ">';
						openhtml +='<span class="pts-prjlist-items-title">'+data[i].toolname+'</span>';
						openhtml +='<br>服务价格：免费';
						openhtml +='</div>&nbsp;&nbsp;&nbsp;';
					}else{
						notopenhtml +='<div serviceId="'+ data[i].id +'" class="pts-prjlist-items-disabled">';
						notopenhtml +='<span class="pts-prjlist-items-title">'+data[i].toolname+'</span>';
						notopenhtml +='<br>服务价格：免费';
						notopenhtml +='</div>&nbsp;&nbsp;&nbsp;';
					}}
				$(".barTitle_opened").append(openhtml);
				$(".barTitle_Notopened").append(notopenhtml);
				$(".pts-prjlist-items").click(showApply);
				$(".pts-prjlist-items-disabled").click(showApply);
			},
		});
		};
    $(function(){
    	sonarservicesGrid = new biz.grid({
    		id : "#sonarservicesTable",
    		url : "sonarservices/listSonarservices",
    		page : "<biz:out value='${page.pageNo}' default='1'/>",
    		rowNum : "<biz:out value='${page.pageSize}' default='15'/>",
			width : "auto",
			multiselect : true,
    		pager : "#sonarservicesPager",
    		prmNames : {page:"pageNo",rows:"pageSize",sort:"orderFields",order:"order"},
    		sortname : "",
    		sortorder : "",
    		colModel : [
			   {name : "id",hidden:true,key:true}, 
				{name : "projectId",hidden:true,formatter:"showlink",formatoptions:{baseLinkUrl:"sonarservices/show"},label:"<fmt:message key="sonarservices.queryResult.projectId"/>"},
				{name : "serviceName",label:"<fmt:message key="sonarservices.queryResult.serviceName"/>"},
				{name : "updateTime",label:"<fmt:message key="sonarservices.queryResult.updateTime"/>"},
				{name : "",label:"服务价格"},
				{name : "",label:"操作"}
    		],
    		serializeGridData:function(postData){//添加查询条件值
    			var obj = biz.utils.serializeObject("#sonarservicesForm");//示例中心搜索"biz.utils方法详解"查看方法说明
    			$.extend(true,obj,postData);//合并查询条件值与grid的默认传递参数
    			return obj;
    		}
    	});
    })

	function query(){
		sonarservicesGrid.setGridParam({"page":"1"});
		sonarservicesGrid.trigger('reloadGrid');
	}
	function add(){
		window.location="sonarservices/add";
	}
	/* 跳转工具服务页面 */
	function showApply(){
		var serviceId = $(this).attr("serviceId");
		$.ajax({
			url: contextPath+"/sonarservices/showApply",
			type:"POST",
			data:{"serviceId":serviceId},
			cache:false,
			async:false,
			success: function(data, textStatus, jqXHR){
				var status = data[0].status;
				switch(status){
					case 0:showopen(serviceId);//未开通，没有开通记录
					break;
					case 1:showrun(serviceId);//已开通未使用，状态为开通，没有使用记录
					break;
				}			
			},
		});
	}
	/* 申请开通工具服务 */
	function showopen(serviceId){
		window.location="sonarservices/showopen/"+serviceId;
	}
	/* 开始使用工具服务 */
	function showrun(serviceId){
		window.location="sonarservices/showrun/"+serviceId;
	}
	/* 开始使用工具服务后查看详情 */
	function showstatus(serviceHistoryId){
		window.location="sonarservices/showstatus?serviceHistoryId="+serviceHistoryId;
	}
	
	function edit(rowid){
		rowid = sonarservicesGrid.getGridParam("selarrrow");
		if(rowid == null || rowid.length == 0){
			showInfo("<fmt:message key='grid.edit.chooseColAlert'/>",3000);
			return ;
		}else if(rowid.length > 1){
			showInfo("<fmt:message key='grid.edit.onlyOne'/>",3000);
			return ;
		}
		var id = sonarservicesGrid.getCell(rowid[0],"id");
		window.location="sonarservices/edit/" + id;
	}

	function view(){
		var rowid = sonarservicesGrid.getGridParam("selarrrow");
		if(rowid == null || rowid.length == 0){
			showInfo("<fmt:message key='grid.view.chooseColAlert'/>",3000);
			return ;
		}else if(rowid.length > 1){
			showInfo("<fmt:message key='grid.view.onlyOne'/>",3000);
			return ;
		}
		var id = sonarservicesGrid.getCell(rowid[0],"id");
		//window.location="sonarservices/show?id=" + id;
		window.location="sonarservices/showstatus?serviceHistoryId="+id;
	}
	function batchDelete(rowid){
		var rowid = sonarservicesGrid.getGridParam('selarrrow');
		if(rowid == null || rowid.length == 0){
			showInfo("<fmt:message key='grid.delete.chooseColAlert'/>",3000);
			return ;
		}
		new biz.alert({type:"confirm",message:I18N.msg_del_confirm,
			title:I18N.promp,callback:function(result){
	    		if (!result) {
	    			return;
	    		}
				var ids="";
				for(var i=0; i < rowid.length;i++){
					var id = sonarservicesGrid.getCell(rowid[i],'id');
					if(i>0){
						ids +=',';
					}
					ids += id;
				}
				window.location="sonarservices/delete/" + ids;
			}
		});
	}
	
</script>
</body>
</html>